package utils

import (
	"excel/types"
	"fmt"

	"github.com/xuri/excelize/v2"
)

func WriteExcel(data []types.ExcelDemo, sheet, path string) {
	f, _ := excelize.OpenFile(path)
	index, _ := f.NewSheet(sheet)
	write(data, sheet, path, f, index)
}

func WriteNewExcel(data []types.ExcelDemo, sheet, path string) {
	f := excelize.NewFile()
	// 创建一个sheet
	index, _ := f.NewSheet(sheet)
	write(data, sheet, path, f, index)
}

func write(data []types.ExcelDemo, sheet, path string, f *excelize.File, index int) {
	f.SetCellValue(sheet, "A1", "姓名")
	f.SetCellValue(sheet, "B1", "性别")
	f.SetCellValue(sheet, "C1", "民族")
	f.SetCellValue(sheet, "D1", "证件类型")
	f.SetCellValue(sheet, "E1", "证件号码")
	f.SetCellValue(sheet, "F1", "手机号")
	f.SetCellValue(sheet, "G1", "出生地省")
	f.SetCellValue(sheet, "H1", "出生地市")
	f.SetCellValue(sheet, "I1", "出生地区")
	f.SetCellValue(sheet, "J1", "籍贯省")
	f.SetCellValue(sheet, "K1", "籍贯市")
	f.SetCellValue(sheet, "L1", "籍贯区")
	f.SetCellValue(sheet, "M1", "生日")
	f.SetCellValue(sheet, "N1", "详细地址")
	f.SetCellValue(sheet, "O1", "工作单位")
	f.SetCellValue(sheet, "P1", "隶属集团")
	f.SetCellValue(sheet, "Q1", "编制状态")
	f.SetCellValue(sheet, "R1", "编制单位（用工单位）")
	f.SetCellValue(sheet, "S1", "编制类型")
	f.SetCellValue(sheet, "T1", "入编时间")
	f.SetCellValue(sheet, "U1", "人员类别")
	f.SetCellValue(sheet, "V1", "入单位时间")
	f.SetCellValue(sheet, "W1", "岗位分类")
	for i, val := range data {
		f.SetCellValue(sheet, fmt.Sprintf("A%d", i+2), val.Name)
		f.SetCellValue(sheet, fmt.Sprintf("B%d", i+2), val.Sex)
		f.SetCellValue(sheet, fmt.Sprintf("C%d", i+2), val.Nation)
		f.SetCellValue(sheet, fmt.Sprintf("D%d", i+2), val.IDType)
		f.SetCellValue(sheet, fmt.Sprintf("E%d", i+2), val.IDNo)
		f.SetCellValue(sheet, fmt.Sprintf("F%d", i+2), val.Mobile)
		f.SetCellValue(sheet, fmt.Sprintf("G%d", i+2), val.Birth1)
		f.SetCellValue(sheet, fmt.Sprintf("H%d", i+2), val.Birth2)
		f.SetCellValue(sheet, fmt.Sprintf("I%d", i+2), val.Birth3)
		f.SetCellValue(sheet, fmt.Sprintf("J%d", i+2), val.Origin1)
		f.SetCellValue(sheet, fmt.Sprintf("K%d", i+2), val.Origin2)
		f.SetCellValue(sheet, fmt.Sprintf("L%d", i+2), val.Origin3)
		f.SetCellValue(sheet, fmt.Sprintf("M%d", i+2), val.BirthDay)
		f.SetCellValue(sheet, fmt.Sprintf("N%d", i+2), val.Address)
		f.SetCellValue(sheet, fmt.Sprintf("O%d", i+2), val.Work)
		f.SetCellValue(sheet, fmt.Sprintf("P%d", i+2), val.Under)
		f.SetCellValue(sheet, fmt.Sprintf("Q%d", i+2), val.Status)
		f.SetCellValue(sheet, fmt.Sprintf("R%d", i+2), val.Unit)
		f.SetCellValue(sheet, fmt.Sprintf("S%d", i+2), val.Type)
		f.SetCellValue(sheet, fmt.Sprintf("T%d", i+2), val.CreatedAt)
		f.SetCellValue(sheet, fmt.Sprintf("U%d", i+2), val.Personal)
		f.SetCellValue(sheet, fmt.Sprintf("V%d", i+2), val.UpdatedAt)
		f.SetCellValue(sheet, fmt.Sprintf("W%d", i+2), val.Job)
	}
	f.SetActiveSheet(index)
	if err := f.SaveAs(path); err != nil {
		fmt.Println(err)
	}
}
